﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Xunit;
using CoolCode.Linq;
using CoolCode.Linq.Expressions;

namespace CoolCode.Tests.Linq.Expressions
{
    public class ExpressionExpandTest
    {
        [Fact]
        public void ExpandableTest()
        {
            Expression<Func<int, bool>> expr = i => i % 2 == 0;
            var exprExpand = expr.Expand();
            var funcExpand = exprExpand.Compile();
            Assert.Equal(false, funcExpand(1));
            Assert.Equal(true, funcExpand(2));
            Assert.Equal(false, funcExpand(3));
            Assert.Equal(true, funcExpand(4));
        }

        public void QueryTest()
        {
            //Expression<Func<Author, bool>> criteria1 = p => p.FirstName != "";
            //Expression<Func<Author, bool>> criteria2 = p => criteria1.Invoke(p)
            //                         || p.FirstName.Contains("2");
            //criteria2 = criteria2.Expand();

            Expression<Func<Author, bool>> criteria2 = p => p.FirstName.Contains("2");
            SQLiteTestDB context = new SQLiteTestDB();

            var query = context.Author.AsExpandable()
                .Where(criteria2)
                .OrderBy(c => c.ID)
                .Take(10);
            var list = query.ToList();
            Assert.Equal(list.Count, 10);
        }
    }
}
